/********************************************************************************
* File Name:	widget.h
* Description:	第15章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtNetwork>
#include <QtGui>
#include "chatwidgit.h"
#include "filesender.h"
#include <qmap.h>

namespace Ui {
    class Widget;
}
//枚举变量标志信息的类型，分别为消息，新用户加入，和用户退出
enum MessageType{Message,NewParticipant,ParticipantLeft,FileName,Refuse,AskStartChat,StartChat,RefuseChat};

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    //获取帐号
    QString getUserName();
    //给Name赋值
    void SetName(QString);


protected:
    void changeEvent(QEvent *e);
    void closeEvent(QCloseEvent *);
    void sendMessage(MessageType type,QString serverAddress="",QString message = "");
    void newParticipant(QString localHostName,QString ipAddress);
    void participantLeft(QString localHostName,QString time);  
    void hasPendingFile(QString localhostname,QString serverAddress,
                        QString clientAddress,QString fileName);

private:
    //保存用户名
    QString Name;
    Ui::Widget *ui;
    QUdpSocket *udpSocket;
    QMap<QString,ChatWidgit *> list;
    qint16 port;
    FileSender * Sender;
    QString getIP();
private slots:
    void on_btnAll_clicked();
    void on_btnCancel_clicked();
    void on_tableWidget_doubleClicked(QModelIndex index);

    void closeFromChat(QString);     
    void sendMessagesToMain(QString,QString);
    void processPendingDatagrams();
    void sentFileName(QString,QString);
    void hasFileToSend(QString,QString);

signals:
    void sendMessagesZ(QString address,QString first,QString second);
};

#endif // WIDGET_H
